import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/storage'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        { name: '黑马智数 - 登录', path: '/login', component: () => import('@/views/Login/Login.vue') },
        {
            path: '/',
            redirect: '/dashboard',
            component: () => import('@/views/Layout/Layout.vue'),
            children: [
                { name: '工作台', path: '/dashboard', component: () => import('@/views/Dashboard/Dashboard.vue') },
                { name: '楼宇管理', path: '/park/building', component: () => import('@/views/ParkManage/BuildingManage/BuildingManage.vue') },
                { name: '企业管理', path: '/park/company', component: () => import('@/views/ParkManage/CompanyManage/CompanyManage.vue') },
                { name: '企业管理表单', path: '/park/company/form', component: () => import('@/views/ParkManage/CompanyManage/Children/CompanyForm.vue') },
                { name: '企业详情', path: '/park/company/detail', component: () => import('@/views/ParkManage/CompanyManage/Children/CompanyDetail.vue') },
                { name: '区域管理', path: '/car/area', component: () => import('@/views/CarManage/AreaManage/AreaManage.vue') },
                { name: '月卡管理', path: '/car/monthly', component: () => import('@/views/CarManage/MonthlyManage/MonthlyManage.vue') },
                { name: '月卡详情', path: '/car/monthly/detail', component: () => import('@/views/CarManage/MonthlyManage/Children/MonthlyForm.vue') },
                { name: '月卡记录', path: '/car/monthly/list', component: () => import('@/views/CarManage/MonthlyManage/Children/MonthlyList.vue') },
                { name: '停车缴费管理', path: '/car/payment', component: () => import('@/views/CarManage/PaymentManage/PaymentManage.vue') },
                { name: '计费规则管理', path: '/car/rules', component: () => import('@/views/CarManage/RulesManage/RulesManage.vue') },
                { name: '物业费管理', path: '/property', component: () => import('@/views/PropertyManage/PropertyManage.vue') },
                { name: '一体杆管理', path: '/railing/manage', component: () => import('@/views/RailingManage/RailingManage/RailingManage.vue') },
                { name: '告警管理', path: '/railing/alarm', component: () => import('@/views/RailingManage/AlarmManage/AlarmManage.vue') },
                { name: '告警详情', path: '/railing/alarm/detail', component: () => import('@/views/RailingManage/AlarmManage/Children/AlarmDetail.vue') },
                { name: '员工管理', path: '/system/employee', component: () => import('@/views/SystemManage/EmployeeManage.vue') },
                { name: '角色管理', path: '/system/roles', component: () => import('@/views/SystemManage/RolesManage.vue') },
            ]
        }
    ]
})

const whiteList = ['/login', '/404']
router.beforeEach((to, from, next) => {
    document.title = to.name + ''
    const token = getToken()
    if (token) {
        if (to.path === '/login') {
            next()
        } else {
            next()
        }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router
